//
//  ExperienceTableViewCell.m
//  MemoryTravel
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "ExperienceTableViewCell.h"

@implementation ExperienceTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews{
    
    self.contentView.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:245 / 255.0 blue:232 / 255.0 alpha:1.0];
    UIView *backgroundView = [UIView new];
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.layer.masksToBounds = YES;
    backgroundView.layer.cornerRadius = 5;
    [self.contentView addSubview:backgroundView];
    
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(0);
    }];
    
    self.backgroundImageView = [UIImageView new];
    [backgroundView addSubview:self.backgroundImageView];
    
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 120, 0));
    }];
    
    UIView *fuckView = [UIView new];
    fuckView.backgroundColor = [UIColor whiteColor];
    fuckView.layer.masksToBounds = YES;
    fuckView.layer.cornerRadius = 33;
    [self.contentView addSubview:fuckView];
    
    [fuckView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(66, 66));
        make.right.mas_equalTo(-30);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-90);
    }];
    
    self.headImageView = [UIImageView new];
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.cornerRadius = 30;
    [fuckView addSubview:self.headImageView];
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(60, 60));
        make.edges.mas_equalTo(UIEdgeInsetsMake(3, 3, 3, 3));
    }];
    
    UILabel *label = [UILabel new];
    NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:@"￥"];
    NSRange range = [@"￥" rangeOfString:@"￥"];
    [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:15 ]} range:range];
    label.attributedText = attrituteString;
    [backgroundView addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(-20);
        make.right.mas_equalTo(-60);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    self.priceLabel = [UILabel new];
    [backgroundView addSubview:self.priceLabel];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(label.mas_right);
        make.top.equalTo(label.mas_bottom).offset(-15);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-10);
    }];
    
    self.titleLabel = [UILabel new];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    self.titleLabel.textColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1.0];
    self.titleLabel.numberOfLines = 0;
    [backgroundView addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.backgroundImageView.mas_bottom).offset(5);
        make.left.mas_equalTo(10);
        make.right.equalTo(self.headImageView.mas_left).offset(-20);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-70);
    }];
    
    self.dateLabel = [UILabel new];
    self.dateLabel.font = [UIFont systemFontOfSize:13];
    self.dateLabel.textColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0];
    [backgroundView addSubview:self.dateLabel];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(10);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
    }];
    
    self.typeLabel = [UILabel new];
    [backgroundView addSubview:self.typeLabel];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.dateLabel.mas_bottom).offset(10);
        make.bottom.equalTo(self.priceLabel.mas_bottom);
        make.right.equalTo(label.mas_left).offset(-20);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.headImageView.userInteractionEnabled = YES;
    [self.headImageView addGestureRecognizer:tap];
}

- (void)setModel:(ExperienceModel *)model{
    if (_model != model) {
        _model = model;
    }
    
    NSURL *url = [NSURL URLWithString:model.title_page];
    [self.backgroundImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@""]];
    
    NSURL *url_ = [NSURL URLWithString:model.anUser.avatar_l];
    [self.headImageView sd_setImageWithURL:url_ placeholderImage:[UIImage imageNamed:@""]];
    
    NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:model.price];
    NSRange range = [model.price rangeOfString:model.price];
    [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:18]} range:range];
    self.priceLabel.attributedText = attrituteString;
    
    self.titleLabel.text = model.title;
    
    if ([model.address isEqualToString:@""]) {
        
        self.dateLabel.text = [NSString stringWithFormat:@"%@   ·   %@人喜欢", model.date_str, model.like_count];
    } else {
        if (model.address.length > 9) {
            NSString *address = [model.address substringToIndex:8];
            self.dateLabel.text = [NSString stringWithFormat:@"%@   ·   %@...   ·   %@人喜欢", model.date_str, address, model.like_count];
        }
        else {
            self.dateLabel.text = [NSString stringWithFormat:@"%@   ·   %@   ·   %@人喜欢", model.date_str, model.address, model.like_count];
        }
    }
    
    [self.typeLabel.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if (model.tab_list.count == 0) {
        
    } else if (model.tab_list.count == 1){
        UILabel *label = [UILabel new];
        label.layer.masksToBounds = YES;
        label.layer.cornerRadius = 3;
        label.layer.borderWidth = 0.5;
        label.layer.borderColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0].CGColor;
        label.textColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0];
        label.font = [UIFont systemFontOfSize:13];
        [self.typeLabel addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.left.mas_equalTo(1);
        }];
        
        label.text = [model.tab_list objectAtIndex:0];
    } else if (model.tab_list.count == 2){
        
        UILabel *label1 = [UILabel new];
        label1.layer.masksToBounds = YES;
        label1.layer.cornerRadius = 3;
        label1.layer.borderWidth = 0.5;
        label1.layer.borderColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0].CGColor;
        label1.font = [UIFont systemFontOfSize:13];
        label1.textColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0];
        [self.typeLabel addSubview:label1];
        
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.left.mas_equalTo(1);
        }];
        
        label1.text = [model.tab_list objectAtIndex:0];
        
        UILabel *label2 = [UILabel new];
        label2.layer.masksToBounds = YES;
        label2.layer.cornerRadius = 3;
        label2.layer.borderWidth = 0.5;
        label2.layer.borderColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0].CGColor;
        label2.font = [UIFont systemFontOfSize:13];
        label2.textColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0];
        [self.typeLabel addSubview:label2];
        
        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(1);
            make.left.equalTo(label1.mas_right).offset(5);
        }];
        
        label2.text = [model.tab_list objectAtIndex:1];
    } else{
        UILabel *label1 = [UILabel new];
        label1.layer.masksToBounds = YES;
        label1.layer.cornerRadius = 3;
        label1.layer.borderWidth = 0.5;
        label1.layer.borderColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0].CGColor;
        label1.font = [UIFont systemFontOfSize:13];
        label1.textColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0];
        [self.typeLabel addSubview:label1];
        
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.left.mas_equalTo(1);
        }];
        
        label1.text = [model.tab_list objectAtIndex:0];
        
        UILabel *label2 = [UILabel new];
        label2.layer.masksToBounds = YES;
        label2.layer.cornerRadius = 3;
        label2.layer.borderWidth = 0.5;
        label2.layer.borderColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0].CGColor;
        label2.font = [UIFont systemFontOfSize:13];
        label2.textColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0];
        [self.typeLabel addSubview:label2];
        
        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(1);
            make.left.equalTo(label1.mas_right).offset(10);
        }];
        
        label2.text = [model.tab_list objectAtIndex:1];
        
        UILabel *label3 = [UILabel new];
        label3.layer.masksToBounds = YES;
        label3.layer.cornerRadius = 3;
        label3.layer.borderWidth = 0.5;
        label3.layer.borderColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0].CGColor;
        label3.font = [UIFont systemFontOfSize:13];
        label3.textColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1.0];
        [self.typeLabel addSubview:label3];
        
        [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(1);
            make.left.equalTo(label2.mas_right).offset(10);
        }];
        
        label3.text = [model.tab_list objectAtIndex:2];
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    [self.expreienceTabelViewCellDeledate pushToWebView:self.model.anUser.person_id];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
